/**
 * Created by dell on 2017/5/12.
 */
import {Component, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Subscription} from "rxjs";
import {MineService} from "@mine/mine.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ToolbarService} from "@shared/components/toolbar/toolbar.service";
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {Subject} from "rxjs/Subject";


@Component({
  selector: 'edit-info',
  templateUrl: 'edit-info.component.html',
  styleUrls: ['edit-info.component.scss'],
  encapsulation: ViewEncapsulation.None
})

export class EditInfoComponent implements OnInit, OnDestroy {
  public defaulticon;
  public sexseed = false; // 性别框
  public agearr: Array<any> = [];
  public heightarr: Array<any> = [];
  public weightarr: Array<any> = [];
  public subscription: Subscription = new Subscription();
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public userObj: any;

  constructor(public mineService: MineService,
              public router: Router,
              public toolservice: ToolsService,
              public authservice: AuthService,
              public route: ActivatedRoute,
              public toolBarService: ToolbarService,
              public imgService: ImageServeService
  ) {
    this.defaulticon = this.imgService.getUserIcon();
  }

  getuserinfo() {
    const userid = this.authservice.getUserId();
    this.mineService.getUserInfo(userid).takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0') {
        this.userObj = res.data;
      };
      this.toolservice.hideLoading();
    });
  }

  // 签名
  editinfo(type) {
    let placeHolder = '';
    if (type === 'nickName') {
      placeHolder = this.userObj.nickName;
    }
    if (type === 'sign') {
      placeHolder = this.userObj.sign;
    }
    const params = {
      type: type
    };
    if (placeHolder) {
      params['placeHolder'] = placeHolder;
    }
    // placeHolder ? params['placeHolder'] = placeHolder : '';
    this.router.navigate(['./mine/setting/edit'], {queryParams: params});
  }

  // 性别
  setGender() {
    this.sexseed = true;
  }

  // 修改性别
  changsexnum(even) {
    const i = parseInt(even.target.id, 10);
    if (i === 0) {
      this.sexseed = false;
    } else {
      this.toolservice.showLoading();
      this.mineService.UserInfoUpdate({sex: i}).takeUntil(this.ngUnsubscribe).subscribe(res => {
        if (res.result === '0') {
          this.sexseed = false;
          this.getuserinfo();
          this.toolservice.showToast('更新性别成功');
        } else {
          this.toolservice.showToast('更新性别失败：' + res.msg, 2000);
          this.router.navigate(['/auth/login']);
        }
        this.toolservice.hideLoading();
      });
    }
  }

  // 头像
  selectFileOnchanged(even) {
    const file = even.target.files[0];
    if (file.size >= 5242880) {
      this.toolservice.showToast('头像大小不可大于5M！');
      return;
    }
    this.toolservice.showLoading();
    this.mineService.updateAvatar(file).takeUntil(this.ngUnsubscribe).subscribe((res) => {
      if (res.result === '0') {
        const reader = new FileReader();
        reader.onload = (r: any) => {
          this.userObj.iconUrl = r.target.result;
        };
        reader.readAsDataURL(file);
        this.toolservice.showToast('更新头像成功');
      } else {
        this.toolservice.showToast('更新头像失败：' + res.msg, 2000);
      }
      this.toolservice.hideLoading();
    });
  }

  age(event) {
    if (typeof event === 'string') {
      this.toolservice.showLoading();
      this.mineService.UserInfoUpdate({age: event}).takeUntil(this.ngUnsubscribe).subscribe(res => {
        if (res.result === '0') {
          this.userObj.userAge = event;
          this.toolservice.showToast('更新年龄成功');
        } else {
          this.toolservice.showToast('更新年龄失败：' + res.msg, 2000);
          this.router.navigate(['/auth/login']);
        }
        this.toolservice.hideLoading();
      });
    }
  }

  height(event) {
    if (typeof event === 'string') {
      this.toolservice.showLoading();
      this.mineService.UserInfoUpdate({height: event}).takeUntil(this.ngUnsubscribe).subscribe(res => {
        if (res.result === '0') {
          this.userObj.height = event;
          this.toolservice.showToast('更新身高成功');
        } else {
          this.toolservice.showToast('更新身高失败：' + res.msg, 2000);
          this.router.navigate(['/auth/login']);
        }
        this.toolservice.hideLoading();
      });
    }
  }

  weight(event) {

    if (typeof event === 'string') {
      this.toolservice.showLoading();
      this.mineService.UserInfoUpdate({weight: event}).takeUntil(this.ngUnsubscribe).subscribe(res => {
        if (res.result === '0') {
          this.userObj.weight = event;
          this.toolservice.showToast('更新体重成功');
        } else {
          this.toolservice.showToast('更新体重失败：' + res.msg, 2000);
          this.router.navigate(['/auth/login']);
        }
        this.toolservice.hideLoading();
      });
    }
  }


  addnum(start, end) {
    const arr = [];
    for (let i = start; i < end; i++) {
      arr.push(i);
    }
    return arr;
  }

  setBallInfo() {
    this.router.navigate(['./mine/my-edit-info/edit-ball-info']);
  }

  chooseCity() {
    this.router.navigate(['/mine/choose-city']);
  }


  ngOnInit() {
    this.agearr = this.addnum(1, 99);
    this.heightarr = this.addnum(50, 220);
    this.weightarr = this.addnum(20, 200);
    this.getuserinfo();
  }

  ngOnDestroy() {
    // 取消订阅
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
    this.toolservice.hideLoading();
  }
}
